package leftMain;
//package org.dlsu.mt.parser;

import java.util.HashSet;
import java.util.Vector;

import org.dlsu.mt.MorphInterface;
import org.dlsu.mt.SentenceTokenizerInterface;
import org.dlsu.mt.SimpleMorphAnalyzer;
import org.dlsu.mt.SimpleSentenceTokenizer;
import org.dlsu.mt.parser.FStructure;
import org.dlsu.mt.parser.FStructureElement;
import org.dlsu.mt.parser.FStructureElementString;
import org.dlsu.mt.parser.GLRParserEngine;
import org.dlsu.mt.parser.LexicalAnalyzerInterface;
import org.dlsu.mt.parser.NLPParser;
import org.dlsu.mt.parser.SimpleLexer;
import org.dlsu.mt.parser.dictionary.Global;
import org.dlsu.mt.parser.generator.GLRParserGenerator;
import org.dlsu.mt.parser.generator.ParseTable;


public class compile
{	
	public static LeftGUI gui;
	
	/*
	public static void main(String args[]) throws Exception
	{
		Global g = new Global();
		testNLPParser('e', "c:/englishwithf.txt", "he left the room $");
		//gui = new LeftGUI(gui);
		
	}*/
	
	public static void main(String args[]) throws Exception
	{		  
		/*
		Dictionary filipinoDictionary = new org.dlsu.mt.parser.Dictionary("Language2");
		Dictionary englishDictionary = new org.dlsu.mt.parser.Dictionary("Language1");
		
		//TransferDictionary transferDictionary = new TransferDictionary();
		
		
		//TransferModule transfer = new TransferModule(englishDictionary, filipinoDictionary, new Global());
		
		Global glob = new Global();
		PrintWriter pw = new PrintWriter("C:/log.txt");
		
		glob.setPrintWriter(pw);
		TransferModule transfer = new TransferModule(englishDictionary, filipinoDictionary, glob);		
		
		//sa palengke pumunta si Marlon
		//ang bata ay mataba -->15
		
		//AnalysisModule analysis = new AnalysisModule("c:/filipino.txt", filipinoDictionary);
		
		//AnalysisModule analysis = new AnalysisModule("c:/language2.txt", filipinoDictionary);
		//FStructure f = analysis.analyze("bumili ng tinapay ang bata at si Grace $");
		//transfer.setDirectionOfTranslation('2');
		
		AnalysisModule analysis = new AnalysisModule("c:/language1.txt", englishDictionary);
		FStructure f = analysis.analyze("he walked to the store $");
		transfer.setDirectionOfTranslation('1');
		
		if(f != null)
		{
			//System.out.println("generation starting....");			
			//f = transfer.translate(f);
			
			GenerationModule gen = new GenerationModule("c:/language1.txt");
			//GenerationModule gen = new GenerationModule("c:/language2.txt");
			System.out.println(gen.generateString(f));
		}
		else
			System.out.println("analysis failed");
		*/
			
		//LEFTTranslator left = new LEFTTranslator();
		
		//left.translateEngToFil("pumunta siya sa bahay ni Marlon");
		
		//System.out.println(left.translateFilToEng("ang bata ay pumunta sa tindahan"));
		
		
		gui = new LeftGUI(gui);
		
		//Global g = new Global();
		//g.transferDictionary.display(g.transferDictionary.parseToFStructure("ROOT FS: ROOT LOC.1 SUBJ COORDVP1 LOC.2 FE: dat noun ay ang_pronoun verb dat noun verb conj FORMAT: LOC.1datnoun SUBJang_pronoun LOC.2datnoun COORDVP1LOC.2verbconj ROOTLOC.1aySUBJverbCOORDVP1"));
		//g.transferDictionary.display(g.transferDictionary.parseToFStructure("ROOT FS: ROOT COMPLEMENT.1 XCOMP.1 OBJOFPREP.1 SUBJ VPCOORD COMPLEMENT.2 XCOMP.2 OBJOFPREP.2 FE: prep det noun pronoun prep det noun verb conj verb FORMAT: OBJOFPREP.1detnoun XCOMP.1prepOBJOFPREP.1 COMPLEMENT.1XCOMP.1 SUBJpronoun OBJOFPREP.2detnoun XCOMP.2prepOBJOFPREP.2 COMPLEMENT.2XCOMP.2 VPCOORDCOMPLEMENT.2verbconj ROOTCOMPLEMENT.1SUBJVPCOORDverb"));
		//System.out.println(g.transferDictionary.getRelatedRule(2, "e"));
			
	}
	
	public static void testNLPParser(char lang, String filename, String toParse) throws Exception {
		Vector v;	
		
		// Generating the Parse Table
		GLRParserGenerator glr= new GLRParserGenerator(filename);

// ----------- Generate the parsing table -----------
		ParseTable pt = glr.createParseTable();
		pt.saveToFile("C:/pt.txt");
// --------------------------------------------------*/
		
/*// ------------------ or load it -------------------		
		ParseTable pt = ParseTable.loadFromFile("C:/pt.txt");
// --------------------------------------------------*/

		//Instantiation of the NLPParser Components
		SentenceTokenizerInterface senTok = new SimpleSentenceTokenizer();
		LexicalAnalyzerInterface lexAnalyzer = new SimpleLexer(" ",false);
		
/*	-----------------------------------------------------	
		DictionaryInterface dict = new SimpleDictionary();
		lexAnalyzer.setDictionary(dict);
		VectoredLexer vl = (VectoredLexer) lexAnalyzer.analyze("det noun verb det noun $");
		while(vl.hasMoreTokens()) {
			Enumeration e = vl.nextToken().getPOSTags();
			while(e.hasMoreElements())
				System.out.println(e.nextElement());
		}
----------------------------------------------------------*/
		
		GLRParserEngine glrEngine= new GLRParserEngine();
		MorphInterface morph = new SimpleMorphAnalyzer();
			
		//DictionaryInterface dict2 = new Dictionary();
		
		//Instantiation of NLPParser
		NLPParser nlpp = new NLPParser();

		//Setting the components of the NLPParser
		nlpp.setLexer(lexAnalyzer);
		nlpp.setParser(glrEngine);
		
		Global glob = new Global();
		
		if (lang == '1')
			nlpp.setDictionary(glob.language1Dictionary);
		else if (lang == '2')
			nlpp.setDictionary(glob.language2Dictionary);
				
		nlpp.setMorph(morph);
		nlpp.setSentenceTokenizer(senTok);
		nlpp.setParseTable(pt);

		//Setting the document to parse
		nlpp.setDocument(toParse);

//System.out.println("TOTAL: " + nlpp.getSentenceTotal());

		// Parsing the document sentence by sentence
/*---------------------------------------------------------
 		PrintWriter pw = new PrintWriter(new FileOutputStream("C:/fstructure.xml"));

		pw.println("<DOCUMENT sentence_count=\""+nlpp.getSentenceTotal()+"\">");
		while (nlpp.hasMoreSentences()) {
			String sentence = nlpp.getNextSentenceToParse();
			v = nlpp.parseSentence();

			pw.println("<SEMANTICS size=\""+v.size()+"\" sentence=\""+sentence+"\">");
			for(int i=0;i<v.size();i++) {
				pw.println(((FStructure) ((Node) v.elementAt(i)).interpret()).toXML());
			}
			pw.println("</SEMANTICS>");
		}
		pw.println("</DOCUMENT>");
		pw.close();
----------------------------------------------------------*/

		//Saves directly to a file
		nlpp.saveParseResultToFile("C:/output.xml","C:/sem.xml");
	}


	public static void testFStructure() throws Exception{
		FStructure f = new FStructure("inside"), f2 = new FStructure("root"), f3= new FStructure("inside2");
		FStructure f4 = new FStructure("inside"), f5 = new FStructure("inside2");
		HashSet h = new HashSet();
		h.add(new FStructureElementString("ABC"));
		h.add(new FStructureElementString("EFG"));

		f.addEntry(new FStructureElement("a",h));

		h = new HashSet();
		h.add(new FStructureElementString("ABCD"));
		h.add(new FStructureElementString("EFG"));
		f.addEntry(new FStructureElement("b",h));

		f2.addEntry(f);

		h = new HashSet();
		h.add(new FStructureElementString("ABCD"));
		h.add(new FStructureElementString("EFG"));
		f2.addEntry(new FStructureElement("b",h));

		h = new HashSet();
		h.add(new FStructureElementString("ABC"));
		h.add(new FStructureElementString("EFG"));
		f3.addEntry(new FStructureElement("a",h));

		h = new HashSet();
		h.add(new FStructureElementString("ABCD"));
		h.add(new FStructureElementString("EFG"));
		f3.addEntry(new FStructureElement("b",h));

		f.addEntry(f3);

		h = new HashSet();
		h.add(new FStructureElementString("ABC"));
		h.add(new FStructureElementString("EFG"));
		f5.addEntry(new FStructureElement("a",h));

		h = new HashSet();
		h.add(new FStructureElementString("ABCD"));
		h.add(new FStructureElementString("EFG"));
		f5.addEntry(new FStructureElement("b",h));

		f4.addEntry(f5);
		f4.addEntry(new FStructureElement("b",h));

		System.out.println(f2.addEntry(f4));

		f2.saveToFile("C:\fstructure.xml");
	}
}